package com.itcodai.utils.lambda;

import com.itcodai.utils.lambda.function.NoNEReturnNoneParameter;
import com.itcodai.utils.lambda.function.SingleReturnMutipleParameter;

import java.util.Arrays;

/**
 * java8 lambda 练习
 *
 * @author c-fanqj
 * @date 2022/5/7
 * @apiNote
 */
public class LambdaExercise {

    public static void main(String[] args) {

        // 数组转成流
        Integer[] array = {1, 3, 5, 7, 9, 0, 8, 6, 4, 2};
        // 倒序
        Arrays.sort(array, (x, y) -> y - x);
        //System.out.println(Arrays.toString(array));
        // [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

        Person[] persons = new Person[5];
        persons[0] = new Person("小明", 9, Gender.Male);
        persons[1] = new Person("小白", 8, Gender.FeMale);
        persons[2] = new Person("小黑", 11, Gender.Male);
        persons[3] = new Person("小绿", 7, Gender.FeMale);
        persons[4] = new Person("小紫", 6, Gender.FeMale);
        // 按照年龄倒序
        //Arrays.sort(persons, (p1, p2) -> p2.getAge() - p1.getAge());
        /*for (Person ele : persons) {
            System.out.println(ele);
        }*/
        /*Person [name=小黑, age=11, gender=Male]
        Person [name=小明, age=9, gender=Male]
        Person [name=小白, age=8, gender=FeMale]
        Person [name=小绿, age=7, gender=FeMale]
        Person [name=小紫, age=6, gender=FeMale]*/

        SingleReturnMutipleParameter lambda = (a, b) -> {
            if (a >= b) {
                return a - b;
            }
            return b - a;
        };
        //System.out.println(lambda.calculate(12, 20)); // 8

        SingleReturnMutipleParameter lambda1 = (a, b) -> LambdaExercise.getAbs(a, b);
        //System.out.println(lambda1.calculate(12, 20)); // 8

        // 普通方法的引用: 静态方法 非静态方法
        SingleReturnMutipleParameter lambda2 = LambdaExercise::getAbs;
        //System.out.println(lambda2.calculate(12, 20));  // 8
        NoNEReturnNoneParameter lambda3 = () -> new LambdaExercise().show();  // hello
        //lambda3.calculate();

        // 构造方法引用
        PerfonMaker lambda4 = () -> new Person();
        PerfonMaker lambda5 = Person::new;
        PerfonMaker2 lambda6 = Person::new;

        // getter、setter的引用
        Dog xiaobai = new Dog();
        xiaobai.setName("xiaobai");
        GetName lamdba7 = x -> x.getName();
        System.out.println(lamdba7.getName(xiaobai));  // xiaobai
        // 特殊方法
        GetName lamdba8 = Dog::getName;
        System.out.println(lamdba8.getName(xiaobai));  // xiaobai

    }

    interface GetName {
        String getName(Dog dog);
    }

    static class Dog {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    interface PerfonMaker2 {
        Person getPerson(String name, int age);
    }

    interface PerfonMaker {
        Person getPerson();
    }

    public void show () {
        System.out.println("hello");
       // return false;
    }

    static int getAbs(int a, int b) {
        if (a >= b) {
            return a - b;
        }
        return b - a;
    }

    enum Gender {
        Male, FeMale    // 男  女
    }

    static class Person {
        private String name;
        private int age;
        private Gender gender;

        public int getAge() {
            return age;
        }

        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", gender=" + gender + "]";
        }

        public Person() {
        }

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public Person(String name, int age, Gender gender) {
            this.name = name;
            this.age = age;
            this.gender = gender;
        }
    }

}
